package cn.toma.service.impl.sys;

import cn.toma.common.utils.web.AddressUtil;
import cn.toma.common.utils.web.HttpContextUtil;
import cn.toma.common.utils.web.IPUtil;
import cn.toma.core.AbstractService;
import cn.toma.model.sys.LoginLog;
import cn.toma.service.sys.LoginLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service("loginLogService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LoginLogServiceImpl extends AbstractService<LoginLog> implements LoginLogService
{

	@Override
	@Transactional
	public void saveLoginLog(LoginLog loginLog)
	{
		loginLog.setLoginTime(new Date());
		HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
		String ip = IPUtil.getIpAddr(request);
		loginLog.setIp(ip);
		loginLog.setLocation(AddressUtil.getCityInfo(ip));
		this.save(loginLog);
	}
}
